//
//  TCLYLoginViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/9/13.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "TCLYLoginViewController.h"
#import "TCLYRegisteredViewController.h"
#import "TCLYRegisterView.h"
#import "TCLYLoginModel.h"
#import "TCLYLoginView.h"
#import "Masonry.h"
#import "Macros.h"
#import "TCLYUser.h"

@interface TCLYLoginViewController ()
@property (nonatomic, strong) TCLYLoginModel * loginModel;
@property (nonatomic, strong) TCLYLoginView * loginView;
@property (nonatomic, strong) TCLYRegisterView * registerView;
@property (nonatomic, assign) BOOL isNews;
@property (nonatomic, assign) BOOL isUpdate;
@end

@implementation TCLYLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(judgeLogin) name:@"judgeLogin" object:nil];
    self.loginModel = [[TCLYLoginModel alloc] init];
    self.registerView = [[TCLYRegisterView alloc] init];
//    self.user = [[TCLYUser alloc] init];
    TCLYUser * users = [[TCLYUser alloc] init];
    NSString * docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString * path = [docPath stringByAppendingPathComponent:@"user.md5"];
//    NSLog(@"path=%@",path);
    users = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
//    NSLog(@"%@%@%@",users.userAccount,users.userPwd,users.userId);
    if (users.userAccount == nil || users.userPwd == nil || users.userId == nil) {
        self.isNews = YES;
        [self createView];
    }else{
        self.isNews = NO;
//        self.view.backgroundColor = [UIColor redColor];
        [self loginImmediately];
    }
}
-(void) createView{
    self.loginView = [[TCLYLoginView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    self.loginView.backgroundColor = bgColor;
    UITapGestureRecognizer * textFeildTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.loginView addGestureRecognizer:textFeildTap];
    [self.loginView.loginBtn addTarget:self action:@selector(loginBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.loginView.registerBtn addTarget:self action:@selector(registerBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginView];
}

-(void)loginBtnAction{
    if ([self.loginView.accountTextField.text isEqual:@""]) {
        [self.registerView prompt:@"账号不能为空" Controller:self];
        return;
    }
    if ([self.loginView.pwdTextField.text isEqual:@""]) {
        [self.registerView prompt:@"密码不能为空" Controller:self];
        return;
    }
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: self.loginView.accountTextField.text,@"phone", self.loginView.pwdTextField.text,@"pass", nil];
    [self.loginModel judgeLoginParameter:dic];
}
-(void)registerBtnAction{
    TCLYRegisteredViewController * registerViewCtr = [[TCLYRegisteredViewController alloc] init];
    [self presentViewController:registerViewCtr animated:YES completion:nil];
}
-(void)judgeLogin{
    NSLog(@"%@",self.loginModel.loginStr);
    if ([self.loginModel.loginStr isEqual:@404] || [self.loginModel.loginStr isEqual:@400]) {
        [self.registerView prompt:@"账号不存在或密码错误" Controller:self];
        if (!self.isNews) {
            self.isUpdate = YES;
            [self.loginView removeFromSuperview];
            [self createView];
        }
        return;
    };
    
    if ([self.loginModel.loginStr isEqual:@401]) {
        [self.registerView prompt:@"账号被冻结" Controller:self];
        if (!self.isNews) {
            self.isUpdate = YES;
            [self.loginView removeFromSuperview];
            [self createView];
        }
        return;
    }
    TCLYUser * users = [[TCLYUser alloc] init];
    if (self.isNews) {
        users.userAccount = self.loginView.accountTextField.text;
        users.userPwd = self.loginView.pwdTextField.text;
        users.userId = self.loginModel.userIdStr;
    }else{
        if (self.isUpdate) {
            NSLog(@"修改之后");
            users.userAccount = self.loginView.accountTextField.text;
            users.userPwd = self.loginView.pwdTextField.text;
            users.userId = self.loginModel.userIdStr;
        }
        
    }
    NSString * docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    //后期要进行md5加密
    NSString * path = [docPath stringByAppendingPathComponent:@"user.md5"];
    [NSKeyedArchiver archiveRootObject:users toFile:path];
    
    //登录成功后进入程序;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:self];
    
}

-(void)loginImmediately{
    TCLYUser * users = [[TCLYUser alloc] init];
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: users.userAccount,@"phone", users.userPwd,@"pass", nil];
    [self.loginModel judgeLoginParameter:dic];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//隐藏键盘的方法
-(void)dismissKeyboard{
    [self.loginView.accountTextField resignFirstResponder];
    [self.loginView.pwdTextField resignFirstResponder];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
